package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUIWindowMenuMain extends JFrame implements MouseListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static final int WINDOW_WIDTH = 900;
	public static final int WINDOW_HEIGHT = 500;
	
	protected JPanel pane = new JPanel();
	protected JLabel backgroundLabel = new JLabel();
	
	protected ImageIcon background;
	protected ImageIcon icon;

	protected JPanel menu = new JPanel();
	private JButton newGame = new JButton("New game");
	private JButton options = new JButton("Options");
	private JButton credits = new JButton("Credits");
	private JButton exit = new JButton("Exit");
	
	public GUIWindowMenuMain()
	{
		this.setTitle("Student wars");
		this.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		this.pane.setBackground(Color.white);
		
		background= new ImageIcon("./data/gfx/gui/logo.png");
		background=GUIWindowMenuMain.resizeImage(background,WINDOW_WIDTH-16,(WINDOW_HEIGHT-16)/3);
		backgroundLabel.setIcon(background);

		pane.setLayout(new FlowLayout(FlowLayout.CENTER,0,5));
		pane.add(backgroundLabel);

		buttonProperties(newGame);
		buttonProperties(options);
		buttonProperties(credits);
		buttonProperties(exit);
		
		menu.setLayout(new GridLayout(4,1,20,20));
		menu.setBackground(Color.white);
		menu.add(newGame);
		menu.add(options);
		menu.add(credits);
		menu.add(exit);
		
		pane.add(menu);
		this.add(pane);
		this.setIconImage(makeIcon().getImage());
	}
	
	public ImageIcon makeIcon()
	{
		icon =  new ImageIcon("./data/gfx/gui/icon.png");
		return icon;
	}
	
	public static ImageIcon resizeImage( ImageIcon tmp, int largeur, int hauteur )
	{
		Image tmpImg = tmp.getImage().getScaledInstance( largeur, hauteur,Image.SCALE_DEFAULT );
		ImageIcon newImage = new ImageIcon( tmpImg );
		return newImage;
	}
	
	public void buttonProperties(JButton button)
	{
		button.setSize(200,40);
		button.setPreferredSize(new Dimension(200,40));
		button.setBackground(Color.white);
		button.addMouseListener(this);
		button.setFocusable(false);
	}
	
	public void buttonOnMouseEntered(JButton button)
	{
		button.setBackground(new Color(165,171,255));
	}
	
	public void mouseClicked(MouseEvent e)
	{
		if(e.getSource() == this.exit)
		{
			System.exit(0);
		}
		else if(e.getSource() == this.credits)
		{
			GUIWindowMenuCredits about = new GUIWindowMenuCredits();
			about.setVisible(true);
		}
		
		else if(e.getSource() == this.newGame)
		{
			this.dispose();
			GUIWindowMenuMultiplayer windowPlayer = new GUIWindowMenuMultiplayer();
			windowPlayer.setVisible(true);
		}
		else if(e.getSource() == this.options)
		{
			this.dispose();
			GUIWindowMenuOptions windowOptions = new GUIWindowMenuOptions();
			windowOptions.setVisible(true);
		}
	}

	public void mouseEntered(MouseEvent e)
	{
		if(e.getSource()==this.newGame)
		buttonOnMouseEntered(newGame);
		if(e.getSource()==this.options)
		buttonOnMouseEntered(options);
		if(e.getSource()==this.credits)
		buttonOnMouseEntered(credits);
		if(e.getSource()==this.exit)
		buttonOnMouseEntered(exit);
	}

	public void mouseExited(MouseEvent e) 
	{
		if(e.getSource()==this.newGame)
		this.newGame.setBackground(Color.white);
		if(e.getSource()==this.options)
		this.options.setBackground(Color.white);
		if(e.getSource()==this.credits)
		this.credits.setBackground(Color.white);
		if(e.getSource()==this.exit)
		this.exit.setBackground(Color.white);
	}
	
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
}